package com.ljtxxhwj.myblob.service.impl;

import com.ljtxxhwj.myblob.entity.Favorite;
import com.ljtxxhwj.myblob.mapper.FavoriteMapper;
import com.ljtxxhwj.myblob.service.FavoriteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class FavoriteServiceImpl implements FavoriteService {

    private final FavoriteMapper favoriteMapper;

    @Autowired
    public FavoriteServiceImpl(FavoriteMapper favoriteMapper) {
        this.favoriteMapper = favoriteMapper;
    }

    @Override
    public List<Favorite> getFavoritesByUserId(Integer userId) {
        return favoriteMapper.selectFavoritesByUserId(userId);
    }

    @Override
    public List<Favorite> getFavoritesByArticleId(Integer articleId) {
        return favoriteMapper.selectFavoritesByArticleId(articleId);
    }

    @Override
    public void addFavorite(Favorite favorite) {
        favorite.setFavoriteTime(LocalDateTime.now());
        favoriteMapper.insertFavorite(favorite);
    }

    @Override
    public void removeFavoriteByArticleAndUser(Integer articleId, Integer userId) {
        favoriteMapper.deleteFavoriteByArticleAndUser(articleId, userId);
    }
}